package com.liang.blog.system.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.liang.blog.common.result.Result;
import com.liang.blog.model.BlogRole;
import com.liang.blog.model.base.BlogPageInfo;
import com.liang.blog.security.annotation.HasPermission;
import com.liang.blog.security.utils.BlogSecurityUtil;
import com.liang.blog.system.service.BlogMenuService;
import com.liang.blog.system.service.BlogRoleService;
import com.liang.blog.vo.menu.MenuSelectVo;
import com.liang.blog.vo.role.RoleInfoVo;
import com.liang.blog.vo.user.UserInfoVo;
import io.swagger.annotations.Api;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Author：YanWenLiang
 * @Date:2023-08-19-22:15
 */
//@Api(tags = "角色模块")
@RestController
@RequestMapping("/role")
public class BlogRoleController {

    @Autowired
    private BlogRoleService blogRoleService;

    @Autowired
    private BlogMenuService menuService;

    @GetMapping("getRolById")
    public Result<BlogRole> getRoleById(){
        return Result.ok();
    }

    /**
     * 获取全部完整角色信息
     * @return
     */
    @GetMapping("getRoleList/{page}/{pageSize}")
    @HasPermission("system:role:read")
    public Result getRoleList(@PathVariable int page, @PathVariable int pageSize,
            RoleInfoVo roleInfoVo){
        PageHelper.startPage(page, pageSize);
        List<RoleInfoVo> blogRoleAll = blogRoleService.getBlogRoleList(roleInfoVo);
        BlogPageInfo<RoleInfoVo> blogRoleAllPage =
                new BlogPageInfo(new PageInfo(blogRoleAll).getTotal(), blogRoleAll);
        return Result.ok(blogRoleAllPage);
    }

    /**
     * 获取全部角色
     * @return
     */
    @GetMapping("getRoleAll")
    @HasPermission("system:role:read")
    public Result getRoleAll(){
        List<RoleInfoVo> blogRoleAll = blogRoleService.getBlogRoleAll();
        return Result.ok(blogRoleAll);
    }

    /**
     * 获取角色绑定的 id
     * @param id
     * @return
     */
    @GetMapping("getRoleBingMenuIdById/{id}")
    @HasPermission("system:role:read")
    public Result getRoleBingMenuIdById(@PathVariable Long id){
        if (id == 1){
            List<Long> blogMenuAll = menuService.getMenuIdAll();
            return Result.ok(blogMenuAll);
        }
        List<Long> roleBingMenuId = blogRoleService.getRoleBingMenuIdById(id);
        return Result.ok(roleBingMenuId);
    }

    /**
     * 修改角色信息
     * @param roleInfoVo
     * @return
     */
    @PostMapping("updateRole")
    @HasPermission("system:role:update")
    public Result updateRole(@RequestBody RoleInfoVo roleInfoVo){
        blogRoleService.updateRoleInfoByRid(roleInfoVo);
        return Result.ok();
    }

    /**
     * 删除用户信息
     * @return
     */
    @PutMapping("deleteRoleById/{id}")
    public Result deleteRoleById(@PathVariable Long id){
        blogRoleService.deleteRoleById(id);
        return Result.ok();
    }
}
